package com.chuangshu.chuangshuteam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chuangshu.chuangshuteam.dto.AssessmentPassListDto;
import com.chuangshu.chuangshuteam.dto.Result;
import com.chuangshu.chuangshuteam.entity.Applications;
import com.chuangshu.chuangshuteam.entity.AssessmentPassLists;
import com.chuangshu.chuangshuteam.entity.UserExportData;
import com.chuangshu.chuangshuteam.mapper.ApplicationsMapper;
import com.chuangshu.chuangshuteam.mapper.AssessmentPassListsMapper;
import com.chuangshu.chuangshuteam.service.AssessmentPassListsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Year;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author k
 * @since 2025-09-21
 */
@Service
public class AssessmentPassListsServiceImpl extends ServiceImpl<AssessmentPassListsMapper, AssessmentPassLists> implements AssessmentPassListsService {
    @Autowired
    private AssessmentPassListsMapper assessmentPassListsMapper;
    @Autowired
    ApplicationsMapper applicationsMapper;

    @Override
    public Result getByPage(Integer page, Integer pageSize) {
        // 创建分页对象
        Page<AssessmentPassLists> passListsPage = new Page<>(page, pageSize);

        // 构建查询条件，按创建时间降序排序
        QueryWrapper<AssessmentPassLists> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("created_at");

        // 执行分页查询
        Page<AssessmentPassLists> pageResult = this.page(passListsPage, queryWrapper);

        // 转换为 DTO 对象
        Page<AssessmentPassListDto> dtoPage = new Page<>(page, pageSize);
        dtoPage.setTotal(pageResult.getTotal());
        dtoPage.setPages(pageResult.getPages());

        List<AssessmentPassListDto> dtoList = new ArrayList<>();
        for (AssessmentPassLists passList : pageResult.getRecords()) {
            AssessmentPassListDto dto = new AssessmentPassListDto();
            BeanUtils.copyProperties(passList, dto);
            dtoList.add(dto);
        }
        dtoPage.setRecords(dtoList);

        return Result.ok(dtoPage);
    }

    @Override
    public AssessmentPassListDto getById(Long id) {
        // 查询通过列表详情
        AssessmentPassLists assessmentPassLists = assessmentPassListsMapper.selectById(id);
        if (assessmentPassLists == null) {
            return null;
        }

        // 转换为 DTO 对象
        AssessmentPassListDto dto = new AssessmentPassListDto();
        BeanUtils.copyProperties(assessmentPassLists, dto);

        return dto;
    }

    @Override
    public List<UserExportData> getPassList() {
        // 创建查询构造器
        LambdaQueryWrapper<AssessmentPassLists> assessmentPassListsLambdaQueryWrapper = new LambdaQueryWrapper<>();

// 1. 添加你已有的查询条件
        assessmentPassListsLambdaQueryWrapper.eq(AssessmentPassLists::getRoundNumber, 3);

// 2. 添加筛选今年的条件
        int currentYear = Year.now().getValue(); // 获取当前年份, 例如 2025
// .apply() 方法可以让你写入原生SQL片段，{0} 是第一个参数的占位符
        assessmentPassListsLambdaQueryWrapper.apply("YEAR(created_at) = {0}", currentYear);

// 3. 执行查询
        List<AssessmentPassLists> assessmentPassLists = assessmentPassListsMapper.selectList(assessmentPassListsLambdaQueryWrapper);

        List<UserExportData> userExportDataList = new ArrayList<>();
        for (AssessmentPassLists assessmentPassList : assessmentPassLists) {
            UserExportData userExportData = new UserExportData();
            userExportData.setName(assessmentPassList.getPassPerson());
            Applications applications = applicationsMapper.selectById(assessmentPassList.getApplicationId());
            userExportData.setDirection(applications.getDirection());
            userExportDataList.add(userExportData);

        }
        return userExportDataList;

    }
}
